package com.scommunity.filter;

import com.scommunity.domain.Emp;
import com.scommunity.domain.User;
import com.scommunity.service.IEmpService;
import com.scommunity.service.IUserService;
import com.scommunity.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@Component
public class LoginFilter extends OncePerRequestFilter {

    @Autowired
    JWTUtils jwtUtils;
    @Autowired
    IEmpService empService;
    @Autowired
    IUserService userService;

//    "/admin/emp/login,/user/loginByAccount,/user/loginByPhone,/user/login/*"

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        //3.判断是否包含登录相关资源路径,同时排除css,js，图片等
        if (requestURI.contains("/admin/image/loadimage") ||requestURI.contains("/admin/emp/login") || requestURI.contains("/user/loginByAccount") || requestURI.contains("/user/loginByPhone") || requestURI.contains("/user/login/")) {
            filterChain.doFilter(request, response);
        } else {
            String token = request.getHeader("token");
            Emp emp = null;
            User user = null;
            if (token != null) {
                //验证token
                Map<String, Long> map = jwtUtils.verifyToken(token);
                Long id = map.get("id");
                switch (map.get("code").intValue()) {
                    //正常
                    case 1:
                        emp = empService.getById(id);
                        if (emp != null) {
                            request.getSession().setAttribute("emp", emp);
                        }

                        user = userService.getById(id);
                        if (user != null) {
                            request.getSession().setAttribute("user", user);
                        }

                        break;
                    //可以续期
                    case 2:
                        //请求正常进行
                        emp = empService.getById(id);
                        if (emp != null) {
                            request.getSession().setAttribute("emp", emp);
                        }
                        request.getSession().setAttribute("emp", emp);
                        //获取一个新的token
                        String newToken = jwtUtils.getToken(emp);
                        //把这个token给响应头
                        response.setHeader("newToken", newToken);

                        //请求正常进行
                        user = userService.getById(id);
                        if (user != null) {
                            request.getSession().setAttribute("user", user);
                        }
                        request.getSession().setAttribute("user", user);
                        //获取一个新的token
                        String newToken1 = jwtUtils.getToken(user);
                        //把这个token给响应头
                        response.setHeader("newToken", newToken1);
                        break;
                    case 3:
                        response.setHeader("code", "406");
                        break;
                    case 4:
                        response.setHeader("code", "406");
                        break;
                }
                filterChain.doFilter(request, response);
            } else {
                response.setHeader("code", "405");
            }
        }


//        filterChain.doFilter(request, response);
    }

}